#include "sysmain.h"

#include <stdio.h>
#include <stdlib.h>
#include "mainwindow.h"
#include "searchwindow.h"
#include "browsewindow.h"
#include "quitwindow.h"

enum CurrentWindow{CWmainwindow, CWsearchwindow, CWbrowsewindow, CWquitwindow};

#define CLOCK_WIDTH    100
#define CLOCK_HRIGHT    40
#define CLOCK_POS_X    (800 - CLOCK_WIDTH)
#define CLOCK_POS_Y    (480 - CLOCK_HRIGHT)
#define CLOCK_FONT_PIXELS 20


static void __event_main(Pos pos);
static void __event_search(Pos pos);
static void __event_browse(bool isReadFirst, Pos pos1, Pos pos2);
static bool __event_quit(Pos pos);

// 初始化系统
static int __init_sysmain(int argc, char* argv[]);

// 开始运行系统
static int __sysmain();

// 销毁系统
static void __destroy_sysmain();

static int __current_window;
static yclock *__clock = NULL;
static bool __isSearch = false;

struct sysmain* create_sysmain()
{
    static struct sysmain *ymain = NULL;

    if (ymain == NULL){
        ymain = (struct sysmain*)malloc(sizeof(struct sysmain));
        if (ymain != NULL){
            ymain->init = __init_sysmain;
            ymain->exec = __sysmain;
            ymain->destroy = __destroy_sysmain;
        }
    }
    return ymain;
}

static int __init_sysmain(int argc, char* argv[])
{
    if (argc <= 1) return -1;
    // 初始化lcd 接口
    if (init_lcd() == false){
        perror("init_lcd error");
        return -1;
    }
    
    // 初始化界面
    init_mainwindow();
    init_searchwindow(argv[1]);
    init_browsewindow();
    init_quitwindow();

    // 创建时钟
    __clock = create_clock(CLOCK_POS_X, CLOCK_POS_Y, CLOCK_WIDTH, CLOCK_HRIGHT, NULL, CLOCK_FONT_PIXELS, 0xffffff, 0x0);
    return 0;
}

static int __sysmain()
{
    // 绘制主界面
    draw_mainwindow(!__isSearch);

    // 当前界面
    __current_window = CWmainwindow;

    // 时钟开始运行
    startClock(__clock);

    // 准备 事件循环变量
    Pos pos1, pos2;
    bool ret = false;
    bool isRun = true;

    // 开始事件循环
    while (isRun){
        // 读取坐标
        ret = read_pos(&pos1, &pos2);

// printf("x1=%d,y1=%d   x2=%d,y2=%d\n", pos1.x, pos1.y, pos2.x, pos2.y);

        switch (__current_window)
        {
        case CWmainwindow:
            __event_main(pos2);
            break;
        case CWsearchwindow:
            __event_search(pos2);
            break;
        case CWbrowsewindow:
            __event_browse(ret, pos1, pos2);
            break;
        case CWquitwindow:
            isRun = !__event_quit(pos2);
            break;
        }
    }

    return 0;
}

static void __destroy_sysmain()
{
    // 销毁时钟
    stopClock(__clock);
    destroy_yclock(__clock);

    // 销毁各界面
    destroy_quitwindow();
    destroy_browsewindow();
    destory_searchwindow();
    destroy_mainwindow();

    // 重置 界面
    clear_lcd(0x0);

    // 关闭lcd接口
    destroy_lcd();

    free(create_sysmain());
}

static void __event_main(Pos pos)
{
    int ret = check_MW(pos.x, pos.y);
    if (__isSearch == false){
        if (ret == MWSearch){
            draw_searchwindow();
            __current_window = CWsearchwindow;
            __isSearch = true;
        }else if (ret == MWQuit){
            draw_quitwindow();
            __current_window = CWquitwindow;
        }
        return ;
    }
    switch (ret)
    {
    case MWSearch:
        draw_searchwindow();
        __current_window = CWsearchwindow;
        break;
    case MWBrowse:
        draw_browsewindow();
        __current_window = CWbrowsewindow;
        break;
    case MWQuit:
        draw_quitwindow();
        __current_window = CWquitwindow;
        break;
    case MWNOT:
        break;
    }
}

static void __event_search(Pos pos)
{
    int ret = check_btn_SW(pos.x, pos.y);
    if (ret == SWQuit){
        draw_mainwindow(!__isSearch);
        __current_window = CWmainwindow;
    }
}

static void __event_browse(bool isReadFirst, Pos pos1, Pos pos2)
{
    int ret = check_btn_BW(isReadFirst, pos1.x, pos1.y, pos2.x, pos2.y);

    if (ret == BWQuit){
        draw_mainwindow(!__isSearch);
        __current_window = CWmainwindow;
    }
    // printf("browse ret == %d\n", ret);
}

static bool __event_quit(Pos pos)
{
    int ret = check_btn_QW(pos.x, pos.y);
    if (ret == QWYes) return true;
    if (ret == QWQuit) {
        draw_mainwindow(!__isSearch);
        __current_window = CWmainwindow;
    }
    return false;
}